//
//  XStatusCell.swift
//  WeiBoReDo
//
//  Created by 放学跟鸽走 on 2017/4/10.
//  Copyright © 2017年 放学跟鸽走. All rights reserved.
//

import UIKit

/// 微博 Cell 的协议
/// 如果需要设置可选协议方法
/// - 需要遵守 NSObjectProtocol 协议
/// - 协议需要是 @objc 的
/// - 方法需要 @objc optional
@objc protocol WBStatusCellDelegate: NSObjectProtocol {
    
    /// 微博 Cell 选中 URL 字符串
    @objc optional func statusCellDidSelectedURLString(cell: XStatusOriginCell, urlString: String)
}

/// 原创微博
class XStatusOriginCell: UITableViewCell {
    
    
    /// 代理属性
    weak var delegate: WBStatusCellDelegate?
    //公共头部
    lazy var topView:XStatusCellTopView = XStatusCellTopView()
    
    // 正文
    lazy var statusLabel: FFLabel = FFLabel.cz_label(withText: "我会放弃吗，搞笑，哈哈哈哈哈哈只带你事合我刚看完个哪个县只带你带你咀哈只带你一234928u4h2i34hjkk", fontSize: 15, color: UIColor.darkGray)//FFLabel
    //配图
    lazy var picView:XStatusPicturesView = XStatusPicturesView()
    //底部工具栏
    lazy var bottomView: XStatusCellBottomView = XStatusCellBottomView()
    
    //配图
    
    //     lazy var picturesView: XStatusPictureView
    //转发微博的正文
    lazy var retweetedLabel:UILabel = UILabel() //FFLabel?
    
    var viewModel:XStatusViewModel?{
        didSet{
            topView.viewModel = viewModel
            statusLabel.attributedText = viewModel?.statusAttrText
            bottomView.viewModel = viewModel
            picView.viewModel = viewModel
            
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
}
// MARK: - UI搭建
fileprivate extension XStatusOriginCell{
    
    /// UI搭建
    func setupUI(){
        //离屏渲染
        self.layer.drawsAsynchronously = true
        //栅格化
        self.layer.shouldRasterize = true
        //使用栅格化必须指定分辨率
        self.layer.rasterizationScale = UIScreen.main.scale
        
        // 设置微博文本代理
        statusLabel.delegate = self
//        retweetedLabel.delegate = self
        selectionStyle = .none
        //        backgroundColor = UIColor.cz_random()
        addViewToCell()
        setupViewInCell()
    }
    /// 添加
    func addViewToCell(){
        addSubview(topView)
        addSubview(statusLabel)
        addSubview(bottomView)
        addSubview(picView)
        
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
    }
    // 原创微博：顶部分隔视图(12) + 间距(12) + 图像的高度(34) + 间距(12) + 正文高度(需要计算) + 配图视图高度(计算) + 间距(12) ＋ 底部视图高度(35)
    // 被转发微博：顶部分隔视图(12) + 间距(12) + 图像的高度(34) + 间距(12) + 正文高度(需要计算) + 间距(12)+间距(12)+转发文本高度(需要计算) + 配图视图高度(计算) + 间距(12) ＋ 底部视图高度(35)
    func setupViewInCell(){
        
        topView.snp.makeConstraints { (make) in
            make.top.equalTo(snp.top)
            make.left.equalTo(snp.left)
            make.right.equalTo(snp.right)
            make.height.equalTo(12+12+34+12)
        }
        
        //        topView.backgroundColor = UIColor.red
        //正文
        statusLabel.snp.makeConstraints { (make) in
            make.left.equalTo(snp.left).offset(StatusCellMargin)
            make.top.equalTo(topView.snp.bottom)
            make.width.equalTo(UIScreen.cz_screenWidth()-2*StatusCellMargin
            )
            
        }
        //配图
        picView.snp.makeConstraints { (make) in
            make.left.equalTo(snp.left).offset(XStatusPictureViewOutterMargin)
            make.right.equalTo(snp.right).offset(XStatusPictureViewOutterMargin)
            make.top.equalTo(statusLabel.snp.bottom)
            make.height.equalTo(90)
//            make.bottom.lessThanOrEqualTo(bottomView.snp.top).offset(XStatusPictureViewOutterMargin)
        }

        //        statusLabel.backgroundColor = UIColor.green
        
        //底部
        bottomView.snp.makeConstraints { (make) in
            make.left.equalTo(snp.left)
            make.right.equalTo(snp.right)
            make.bottom.equalTo(snp.bottom)
            make.height.equalTo(35)
        }
        
    
        
    }
    
}
extension XStatusOriginCell: FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        // 判断是否是 URL
        if !text.hasPrefix("http://") {
            return
        }
        
        // 插入 ? 表示如果代理没有实现协议方法，就什么都不做
        // 如果使用 !，代理没有实现协议方法，仍然强行执行，会崩溃！
        delegate?.statusCellDidSelectedURLString?(cell: self, urlString: text)
    }
}
